<template>
    <van-popup
        v-model:show="showPopup" :style="{ height: '90%' }" position="bottom" round @closed="onPopupClosed"
        @opened="onPopupOpened"
    >
        <div class="publish-strategy-selecter">
            <div class="fe-title">
                <h3>请选择投放策略</h3>
                <img alt="" src="@/assets/img/close-popup.png" @click="onPopupClosed" />
            </div>
            <!-- <div class="tip-text">
                同一楼盘优先选择CPH更低的媒体形式
            </div> -->
            <div class="list">
                <div
                    v-for="item in list" :key="item.name" :class="checkItem.id === item.id && 'item-check'"
                    class="item" @click="changeStrategy(item)"
                >
                    <img v-if="item.optimal" class="best" src="@/assets/img/best-option.png">
                    <img v-if="checkItem.id === item.id" class="check" src="@/assets/img/check.png">
                    <h1>{{ item.name }}</h1>
                    <div class="desc">
                        <img src="@/assets/img/cph-tips.png" alt="">
                        <span>{{ item.desc }}</span>
                    </div>
                    <div class="pro-list">
                        <span>投放</span>
                        <span v-for="pro in item.proList" :key="pro.name">{{ $numberWithCommas(pro.amount) + pro.unit + pro.name + '，' }}</span>
                        <span>覆盖{{ $numberWithCommas(item.coveredPeopleNum) }}人</span>
                    </div>
                    <div class="total-amount">
                        <span class="amount">¥{{ formatNumber(item.total || '') }}</span>
                        <span>（按刊例价的0.5折计算）</span>
                    </div>
                    <div class="cph-price">
                        <span>CPH：</span>
                        <span class="price">{{ item.cph }}元/户/周</span>
                    </div>
                </div>
            </div>
            <div class="fe-bottom" :class="getBottomClass()">
                <div class="confirm" @click="onConfirm">
                    确定
                </div>
            </div>
        </div>
    </van-popup>
</template>

<script lang="ts" setup>
import {ref, watch, PropType} from 'vue';
import {Popup as VanPopup} from 'vant';
import {formatNumber} from '@/utils/filters';
import {isIos} from '@/utils';
import {StrategyItemModel} from '@/views/products/brandBreakout/breakout.module';

const props = defineProps({
    show: {
        type: Boolean,
        require: true,
        default: false,
    },
    list: {
        type: Array as PropType<StrategyItemModel[]>,
        require: true,
        default: () => [],
    },
    strategyId: {
        type: String,
        require: false,
        default: '',
    },
});

const emit = defineEmits<{(e: 'close'): void; (e: 'confirm', value: StrategyItemModel): void; }>();
const checkItem = ref<StrategyItemModel>({id: '', name: ''});

const showPopup = ref(false);

const changeStrategy = (item: StrategyItemModel) => {
    checkItem.value = item;
};
const initData = () => {
    checkItem.value = props.list.find(item => item.id === props.strategyId) || {id: '', name: ''};
};

const onPopupOpened = () => {
    // initData();
};
const onPopupClosed = () => {
    emit('close');
};

function getBottomClass() {
    return isIos() ? 'is-ios' : '';
}

const onConfirm = () => {
    emit('confirm', checkItem.value as StrategyItemModel);
    emit('close');
};

watch(
    () => props.show,
    (newVal) => {
        showPopup.value = newVal;
    }
);

watch(
    () => props.list,
    () => {
        initData();
    }, {
        deep: true
    }
);
</script>

<style lang="scss" scoped>
@import "@/styles/variables";

.publish-strategy-selecter {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100%;

    .fe-title {
        display: flex;
        padding: 0 12px 0 32px;
        margin-top: 20px;

        h3 {
            height: 22px;
            font-weight: 600;
            font-size: 16px;
            color: #333;
            text-align: center;
            flex: 1;
        }

        img {
            width: 20px;
            height: 20px;
        }
    }

    .tip-text {
        margin-top: 15px;
        padding-left: 12px;
        height: 38px;
        line-height: 38px;
        background: #fef7e8;
        font-weight: 400;
        font-size: 13px;
        color: #ffa033;
    }

    .list {
        padding: 12px;
        overflow: auto;
        flex: 1;

        .item {
            background: #ffffff;
            box-shadow: 0 2px 10px 0 #00000014;
            border-radius: 10px;
            position: relative;
            padding: 14px 12px 12px;
            border: 1px solid transparent;

            .best {
                width: 48px;
                height: 48px;
                position: absolute;
                top: 12px;
                right: 12px;
            }

            .check {
                width: 22px;
                height: 22px;
                position: absolute;
                right: -1px;
                bottom: -1px;
            }

            h1 {
                height: 21px;
                font-weight: 600;
                font-size: 15px;
                color: #333333;
            }

            .desc {
                display: flex;
                align-items: center;
                margin-top: 4px;
                img {
                    width: 14px;
                    height: 14px;
                }
                span {
                    font-weight: 400;
                    font-size: 12px;
                    color: #b5b5b5;
                    margin-left: 8px;
                }
            }

            .pro-list {
                margin-top: 5px;

                span {
                    display: inline-block;
                    height: 17px;
                    font-weight: 400;
                    font-size: 12px;
                    color: #5b5b5b;
                    text-align: right;
                }

                // span:not(:last-child) {
                //     margin-right: 12px;
                // }
            }

            .total-amount {
                margin-top: 10px;
                display: flex;
                justify-content: flex-end;
                align-items: center;

                span {
                    display: inline-block;
                    font-weight: 400;
                    font-size: 12px;
                    color: #5b5b5b;
                }

                .amount {
                    height: 21px;
                    font-family: $spec_font;
                    font-size: 18px;
                    color: #e60044;
                    text-align: right;
                }
            }

            .cph-price {
                margin-top: 5px;
                display: flex;
                justify-content: flex-end;
                align-items: center;

                span {
                    display: inline-block;
                    font-weight: 400;
                    font-size: 12px;
                    color: #5b5b5b;
                    height: 17px;
                }

                .price {
                    height: 17px;
                    font-weight: 600;
                    font-size: 12px;
                    color: #333333;
                    margin-right: 6px;
                }
            }
        }

        .item-check {
            border: 1px solid #e60044;
        }

        .item:not(:last-child) {
            margin-bottom: 12px;
        }
    }

    .fe-bottom {
        background: #ffffff;
        box-shadow: 0 -2px 4px 0 #ebebeb80;
        padding-top: 8px;
        // padding-bottom: 39px;

        .confirm {
            margin: 0 12px;
            height: 38px;
            line-height: 38px;
            background: #e60044;
            border-radius: 6px;
            font-weight: 600;
            font-size: 14px;
            color: #ffffff;
            text-align: center;
            margin-bottom: 8px;
        }
    }

    .is-ios {
        padding-bottom: contain(safe-area-inset-bottom);
        padding-bottom: env(safe-area-inset-bottom);
    }
}
</style>

